// pages/module/addAddress/addAddress.js
import regeneratorRuntime from '../../../utils/runtime.js';
import img_list from '../../../utils/util.js';
import { getData, setData, clearData, dataKey } from '../../../utils/storage.js';
import shareFun from '../../../utils/shareFun.js';
import {
  saveAddress,
  cherkAddress,
} from '../../../utils/imapi.js';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    buy_user_address: '江苏省苏州市沧浪区 沧浪区盘溪新村51幢602室',
    buy_user_mobile: '188****4657',
    buy_user_name: '朱丽萍',
    address_id: '',
    img_list:"img_list.img_list"
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad:async function (options) {

    shareFun.hidenShareMenu();

    const {data} = await cherkAddress();
	
    this.setData({
      address_id:data[0].id,
      buy_user_address: data[0].rec_address_detail,
      buy_user_name: data[0].rec_name,
      buy_user_mobile: data[0].rec_phone
    })
    this.setData({
      img_list:img_list.img_list
    })
  },
  // 添加地址
  addAdress: function () {
    var that = this;
    console.log(that.data.address_id)
    wx.chooseAddress({
      success:async function (res) {
        var rec_address_detail = res.provinceName + res.cityName + res.countyName + res.detailInfo;
        that.setData({
          buy_user_address: res.provinceName + res.cityName + res.countyName + res.detailInfo,
          buy_user_name: res.userName,
          buy_user_mobile: res.telNumber
        })
		//console.log(that.data)
		//console.log(that.data.address_id)
        const {data} = await saveAddress({
          id:that.data.address_id,
          rec_name:res.userName,
          rec_phone:res.telNumber,
          rec_province:res.provinceName,
          rec_city:res.cityName,
          rec_address_detail:rec_address_detail,
        })
		// const {data} = await saveAddress({
		//   id:1298963737218121728,
		//   rec_name:"李四",
		//   rec_phone:"14111144441",
		//   rec_province:"江苏省",
		//   rec_city:"常州市",
		//   rec_address_detail:"江苏省常州市新北区哈哈"
		// })
        that.setData({
          address_id:data.id
        })
        wx.showToast({
          title: '地址添加成功',
        })
      },
      fail: function (err) {

      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})